﻿//导入表定义
document.write("<script language='javascript' src='/resources/js/views/commonFunction.js'></script>");
document.write("<script language='javascript' src='/resources/js/views/defineTable.js'></script>");
document.write("<script language='javascript' src='/resources/js/views/defineInit.js'></script>");
document.write("<script language='javascript' src='/resources/js/preAlarm/preAlarmCommon.js'></script>");

//导入表定义
var iHeight = document.documentElement.clientHeight-200;//判断表格自适应高度
var page = 1;
var pageSize = 200;

var action_prealarm_province = "/report/prealarm/province.do";
var host_path_action_department_query_province = '/department/query/province.do';


//清除图形and表格数据
function clearDataPrivince(){
	$("#view_containerzhuzi").html('');   //清空图形
	$("#view_containerzhuzi").height("0px");
	$("#view_containerzhuzi").width("0px");
	$('#tt').datagrid({data:[]});
}

function query(curPage, pageSize)  {
	
	curPage = parseInt(curPage);
	pageSize = parseInt(pageSize);
	$('#page').val(curPage);
	$('#pageSize').val(pageSize);

	var reportShowColumns = getreportShowColumns();

	var indicatorType= $('#indicator_type_select').combobox("getValues") + "";
	var departIds= $('#annualAnalysis_departmentSelect').combobox("getValues")+ "";

	var iname = $('#iname').textbox('getValue');

	var years= $('#comboboxYear').combobox("getValues")+ "";
	// var years = $('#comboboxYear').combobox('getText');
	// var yearList = [];
	// if(years!=null ||years!='' || years!=undefined){
	// 	yearList=years.split(',');
	// }

	var formid=$("#formid").combobox("getValue");
	$.get(action_prealarm_province, {"iname":iname, "indicatorType": indicatorType, "departId": departIds, "years": years, "page": curPage, "pageSize": pageSize,"formsid":formid},

		function (json) {
		if(json.code!=200) {
			clearDataPrivince();
			return;
		}

		var data = json.data;
		var total = data["size"];
		if(total==""){
			 $('#tt').datagrid({data:[]});
			 $("#view_containerzhuzi").height("0px");
			 $("#view_containerzhuzi").width("0px");
			 return;
		}

		var totalPage = Math.ceil(total / pageSize);
		var start = (curPage - 1) * pageSize;
		var dataLength = 0;

		data = data["results"];
		if (data != null && data.length > 0) {
			dataLength = data.length;
		}

		$('#tt').datagrid( {
			height:iHeight,
			frozen:true,
			fitColumns: true,
			striped: true,  //显示条纹，隔行换色
			columns: [
				reportShowColumns
			],
			singleSelect: true,// 是否单选
			loadMsg: '数据加载中,请稍后......',
			pageNumber: curPage,//显示数据列用
			pageSize: pageSize,
			pageList: [200, 500, 1000],
			rownumbers: true,
			pagination: true,//打开翻页功能
			sortable:true,
			sortName:"code",//排序
			multiSort:true, //定义是否允许多列排序
			remoteSort:false,//表示前端排序不请求服务器
			sortOrder:'asc',
			data: data
		});

		var p = $('#tt').datagrid('getPager');
		$(p).pagination({
			total: total,
			pageList: [200, 500, 1000],
			pageSize: pageSize,
			pageNumber: curPage,
			beforePageText: '第',
			afterPageText: '页    共 ' + totalPage + ' 页',
			displayMsg: '当前显示 ' + (start + 1) + '-' + (start + dataLength) + ' 条记录 共 ' + total + ' 条记录',
			onSelectPage: function (curPage, pageSize) {
                query(curPage, pageSize);
			},
			onChangePageSize: function (ps) {
				pageSize = ps;
			}
		});
		//画图
		checkChangeImage(data, years);
		try {
			window.parent.unmask();
		} catch (err) {

		}
	}, "json");
}

var show = {
    tableshow:function () {
        show.a = !show.a;
        if(show.a){
            $("#view_chart").panel("expand",true);
            $("#tt").datagrid("resize",{
                height:iHeight
            });
        }
    },
    photoshow:function () {
        var iHeight = document.documentElement.clientHeight-120;
            $("#tt").datagrid("resize",{
                height:iHeight
            });
        show.a = false;
    },
	 chartshow:function(){
        $("#tt").datagrid("resize",{
           height:iHeight
        });
        show.a = true;
    }
};

//复选框点击事件
function checkyears(){
    CRUDProvice.queryList();
}
var CRUDProvice = {
	queryList: function () {
        query(page,pageSize);
	}
};

/*
 * rgb颜色转换
 */
function zero_fill_hex(num, digits) {
	var s = num.toString(16);
	while (s.length < digits){
		s = "0" + s;
	}
	return s;
}

function rgb2hex(rgb) {
  if (rgb.charAt(0) == '#'){
	  return rgb;
  }
  var ds = rgb.split(/\D+/);
  var decimal = Number(ds[1]) * 65536 + Number(ds[2]) * 256 + Number(ds[3]);
  return "#" + zero_fill_hex(decimal, 6);
}

var show = {
    tableshow:function () {
      // alert(1);
    },
    photoshow:function () {
    	//alert(2);
    },
    chartshow:function(){
    	var time= setTimeout(function(){
			showData(series, jsonXData);
		},10);
    }
};

//------------------------------------------------------------------省级成员单位-------------------------------------------------------------
function localProvinceDepart() {
	$.ajax({
		url:host_path_action_department_query_province,
		data:"",
		type: 'GET',
		dataType:"json",
		contentType:"application/json;charset=UTF-8",

		success: function (json) {
			json.data.unshift({
				id: -1,
				name: "全部"
			});
			$('#annualAnalysis_departmentSelect').combobox({
				async : false,
				method : 'GET',
				data : json.data,
				valueField : 'id',
				textField : 'name',
				fitColumns:false,
				collapsible: true,
				singleSelect : false,// 是否单选
				checkOnSelect: true,//选中行后,checkbox选中
				selectOnCheck: true,//选中行后,checkbox选中
				multiple:true,
				editable:true,
				formatter:function(row){
					var opts;
					if(row.selected==true){
						opts = "<input type='checkbox' checked='checked' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
					}else{
						opts = "<input type='checkbox' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
					}
					return opts;
				},
				onLoadSuccess: function () { // 数据加载完毕事件
					var a = $('#annualAnalysis_departmentSelect').combobox('getData');
					if (a.length > 0) {
						$("#annualAnalysis_departmentSelect").combobox('select', a[0].id);
					}
				},
				onSelect: function () {
                    query(page, pageSize);
				}
			})
		}
	});
}

$(function(){

	$("#tabDiv").height(iHeight+40);
	$(".panel-body").height('100%');
	$('#tablepanel').panel({
		title: '数据列表',
	});

	$('#tt').datagrid( {
		width : '100%',
		height : iHeight
	});

    // 页面加载之后再执行其他的！！！
    window.onload = function (ev) {

		loadIndicatorTypeAll();
		$("#color_memo_html").html(color_memo_html);

		loadYear();
        $('#tabDiv').tabs({
            height:'700px',
            onSelect:function(title,index){
                if(index==0){

                }else if(index==1){
                    var time= setTimeout(function(){
                        showData(series, jsonXData);
                        showImage(0, "PAGETYPE_PROVINCE")
                    },10);
                }
            }
        });
        $("#tabDiv").width($("#content").width());

        localProvinceDepart();
        var years=document.getElementsByName("year");    //复选框年份个数
        showImage((years.length)-1, "PAGETYPE_PROVINCE");
    }

});
